package com.jintian.smart.kernel.autoconfigure.beetl;

import com.jintian.smart.kernel.autoconfigure.ConfigConstants;
import com.jintian.smart.kernel.common.util.StringUtil;
import lombok.Data;
import org.beetl.sql.clazz.kit.StringKit;
import org.beetl.sql.core.ConditionalSQLManager;
import org.beetl.sql.core.UnderlinedNameConversion;
import org.beetl.sql.core.db.OracleStyle;
import org.springframework.core.env.Environment;

import java.util.LinkedHashMap;
import java.util.Map;

@Data
public class BeetlSqlProperties {
    SQLManagerConfig common;
    Map<String, SQLManagerConfig> sqlManagers = new LinkedHashMap<>();

    public void init(Environment env) {
        this.common = SQLManagerConfig.initDefault(env);
        String[] allSqlManangerNames = null;
        String oldSqlManagerConfig = env.getProperty(ConfigConstants.PROPERTIES_PREFIX_BEETLSQL);
        if (StringUtil.isNotEmpty(oldSqlManagerConfig)) {
            allSqlManangerNames = oldSqlManagerConfig.split(",");
        } else {
            if (StringUtil.isEmpty(env.getProperty(ConfigConstants.PROPERTIES_PREFIX_BEETLSQL + ".sqlManagers"))) {
                throw new IllegalArgumentException("缺少" + ConfigConstants.PROPERTIES_PREFIX_BEETLSQL + ".sqlManagers" + "配置");
            }
            allSqlManangerNames = env.getProperty(ConfigConstants.PROPERTIES_PREFIX_BEETLSQL + ".sqlManagers").split(",");
        }
        for (String s : allSqlManangerNames) {
            SQLManagerConfig sqlManagerConfig = new SQLManagerConfig(env, s, this.common);
            sqlManagers.put(s, sqlManagerConfig);
        }
    }

    @Data
    public static class Dynamic {
        String sqlManagers;
        String condition;
    }

    @Data
    public static class SQLManagerConfig {
        private String basePackage;// 配置beetlsql.daoSuffix来自动扫描com包极其子包下的所有以Dao结尾的Mapper类
        private String daoSuffix;// 通过类后缀 来自动注入Dao
        private String sqlPath;// 存放sql文件的根目录
        private String sqlFileCharset;// 文件的字符集

        private String nameConversion;// 数据库和javapojo的映射关系
        private String dbStyle; // 何种数据库
        private Boolean dev;
        private String ds;
        private Dynamic dynamic;

        public SQLManagerConfig() {
        }

        public SQLManagerConfig(Environment env, String sqlManagerName, SQLManagerConfig defaultConfig) {
            String prefix = ConfigConstants.PROPERTIES_PREFIX_BEETLSQL + "." + sqlManagerName;
            String dynamicSqlManager = env.getProperty(prefix + ".dynamic.sqlManagers");
            if (StringUtil.isNotEmpty(dynamicSqlManager)) {
                this.dynamic = new Dynamic();
                this.dynamic.sqlManagers = dynamicSqlManager;
                String dynamicCondition = env.getProperty(prefix + ".dynamic.condition");
                if (StringKit.isEmpty(dynamicCondition)) {
                    // 默认
                    dynamicCondition = ConditionalSQLManager.DefaultConditional.class.getName();
                }
                this.dynamic.condition = dynamicCondition;
                basePackage = env.getProperty(prefix + ".basePackage", defaultConfig.getBasePackage());
                daoSuffix = env.getProperty(prefix + ".daoSuffix", defaultConfig.getDaoSuffix());
                dev = env.getProperty(prefix + ".dev", Boolean.class, defaultConfig.getDev());
                return;
            }
            basePackage = env.getProperty(prefix + ".basePackage", defaultConfig.getBasePackage());
            daoSuffix = env.getProperty(prefix + ".daoSuffix", defaultConfig.getDaoSuffix());
            sqlPath = env.getProperty(prefix + ".sqlPath", defaultConfig.getSqlPath());
            sqlFileCharset = env.getProperty(prefix + ".sqlFileCharset", defaultConfig.getSqlFileCharset());
            nameConversion = env.getProperty(prefix + ".nameConversion", defaultConfig.getNameConversion());
            dbStyle = env.getProperty(prefix + ".dbStyle", defaultConfig.getDbStyle());
            dev = env.getProperty(prefix + ".dev", Boolean.class, defaultConfig.getDev());
            ds = env.getProperty(prefix + ".ds");
            if (ds == null) {
                throw new NullPointerException(prefix + ".ds 不能为空");
            }
        }

        public static SQLManagerConfig initDefault(Environment env) {
            String prefix = ConfigConstants.PROPERTIES_PREFIX_BEETLSQL_DEFAULT;
            SQLManagerConfig defaultConfig = new SQLManagerConfig();
            if (env.containsProperty(prefix + ".daoSuffix"))
                defaultConfig.setDaoSuffix(env.getProperty(prefix + ".daoSuffix", "Mapper"));
            if (env.containsProperty(prefix + ".basePackage"))
                defaultConfig.setBasePackage(env.getProperty(prefix + ".basePackage"));
            if (env.containsProperty(prefix + ".sqlPath"))
                defaultConfig.setSqlPath(env.getProperty(prefix + ".sqlPath", "sql"));
            if (env.containsProperty(prefix + ".nameConversion"))
                defaultConfig.setNameConversion(env.getProperty(prefix + ".nameConversion", UnderlinedNameConversion.class.getName()));
            if (env.containsProperty(prefix + ".sqlFileCharset"))
                defaultConfig.setSqlFileCharset(env.getProperty(prefix + ".sqlFileCharset", "utf-8"));
            if (env.containsProperty(prefix + ".dbStyle"))
                defaultConfig.setDbStyle(env.getProperty(prefix + ".dbStyle", OracleStyle.class.getName()));
            if (env.containsProperty(prefix + ".dev"))
                defaultConfig.setDev(env.getProperty(prefix + ".dev", Boolean.class, true));
            return defaultConfig;
        }

        public void withDefault(SQLManagerConfig defaultConfig) {
            if (StringUtil.isEmpty(this.getDaoSuffix())) {
                this.setDaoSuffix(defaultConfig.getDaoSuffix());
            }
            if (StringUtil.isEmpty(this.getBasePackage())) {
                this.setBasePackage(defaultConfig.getBasePackage());
            }
            if (StringUtil.isEmpty(this.getSqlPath())) {
                this.setSqlPath(defaultConfig.getSqlPath());
            }
            if (StringUtil.isEmpty(this.getNameConversion())) {
                this.setNameConversion(defaultConfig.getNameConversion());
            }
            if (StringUtil.isEmpty(this.getSqlFileCharset())) {
                this.setSqlFileCharset(defaultConfig.getSqlFileCharset());
            }
            if (StringUtil.isEmpty(this.getDbStyle())) {
                this.setDbStyle(defaultConfig.getDbStyle());
            }
            if (this.getDev() == null) {
                this.setDev(defaultConfig.getDev());
            }
        }
    }
}
